import { getList, saveList, deleteList, updates } from "@/api/msbd"

let state = {
  tableData: [],
  classify: [],
  currentPage: 1,
  pageSize: 5,
  searchVal: "",
  sort: "desc", // desc OR asc
  orderby: "id",
  total: 0
}

let mutations = {
  SET_LIST(state, res) {
    state.tableData = res.data,
      state.classify = res.classify,
      state.total = res.total[0].total
  },
  // 当前页  
  SET_PAGE(state, page) {
    state.currentPage = page
  },
  // 每页项数
  SET_SIZE(state, page) {
    state.pageSize = page
  },
  // 搜索
  SET_SEARCH(state, val) {
    state.searchVal = val
  },
}

let actions = {
  async getTableData({ commit, state }) {
    let data = await getList({
      sort: state.sort,
      orderby: state.orderby,
      currentPage: state.currentPage - 1,
      pageSize: state.pageSize,
      searchVal: state.searchVal
    })
    // console.log(data);
    commit("SET_LIST", data)
  },
  // new - add
  async saveFormList({ dispatch }, data) {
    await saveList(data)
    dispatch("getTableData")
  },
  // 删除
  async removeList({ dispatch }, ids) {
    await deleteList(ids)
    dispatch("getTableData")
  },
  // 修改
  async updateList({ dispatch }, data) {
    console.log(data);
    await updates(data)
    dispatch('getTableData')
  },
}

let getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}